<?php
/**
 * Zhub - A PHP Framework For API Developer
 *
 * This file is part of the Zhub package.
 *
 * (c) YiLong Fan <fantiq@163.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author fanyilong <fantiq@163.com>
 * @copyright 2017 fanyilong. All rights reserved.
 * @package Zhub
 * @date 18-3-19
 */

namespace Zhub\Swoole\Server\Http;


use App\MainRoute;
use Zhub\Application\Http\Kernel;
use Zhub\Route\Route;

class Server
{
    private $server = null;

    public function __construct(int $port = 9501, $host = '127.0.0.1', array $config = [])
    {
        $this->server = new \swoole_http_server($host, $port);
        $this->server->set($config);

        $this->server->on('request', function ($request, $response) {

            $app = new Kernel(
                new SwooleHttpServer($request, $response),
                Route::getInstance(MainRoute::class));

            $app->handle();
        });
    }

    public function run()
    {
        $this->server->start();
    }
}
